const schemaJoi = require('./util/schemaJoi.js');
const Joi = require('joi');
const service = require('./service/service.js');

// // 添加用户
exports.setUser = async (context) => {
  const { cloud, data, mixNick = "test", userNick = "test", openId } = context
  try {
    const params = {
      mixnick: mixNick,
      nickName: userNick
    }
    const schema = Joi.object().keys({
      nickName: Joi.string().required(),
      mixnick: Joi.string().required(),
    });
    await schemaJoi(schema, params);
    return await service.addUser({ ...params, openId }, cloud);
  } catch (e) {
    return { isOk: 0, msg: '铺捉到错误' + e };
  }
};



// 加购
exports.addCartOder = async (context) => {
  const { cloud, data, mixNick = 'test', userNick = 'test', openId } = context
  try {
    const params = {
      nickName: userNick || '没授权',
      mixnick: mixNick,
      ...data
    }
    const schema = Joi.object().keys({
      // id: Joi.Number().required(),
      cart_data: Joi.string().required(),
      sample_data: Joi.string().required(),
      nickName: Joi.string().required(),
      mixnick: Joi.string().required(),
    });
    await schemaJoi(schema, params);
    return await service.addCart({ ...params, openId }, cloud);
  } catch (e) {
    return { errCode: 1111, msg: '铺捉到错误' + e };
  }
};



/*
* 添加埋点信息 
* @return data: object 埋点的数据
* */
exports.addUserStat = async (context) => {
  const { cloud, data, mixNick } = context
  console.log("============================", data)
  try {
    //参数验证
    const schema = Joi.object().keys({
      stat_data: Joi.object().required(),
    });
    await schemaJoi(schema, data);
    let paramsData = data.stat_data;
    paramsData.userId = mixNick;
    return await service.addUserStat(JSON.stringify(paramsData), cloud);
  } catch (e) {
    return { isOk: 0, msg: '铺捉到错误' + e };
  }
};




/*
* 系订单
* @return data: object 埋点的数据
* */
exports.matchOrder = async (context) => {
  const { cloud, data, mixNick, openId } = context
  try {
    let date = new Date()
    return await service.matchOrder({ date }, cloud);
  } catch (e) {
    return { isOk: 0, msg: '铺捉到错误' + e };
  }
};


/*
* 系订单
* @return data: object 埋点的数据
* */
exports.setOrder = async (context) => {
  const { cloud, data, mixNick, openId } = context
  try {
    let date = new Date()
    return await service.setOrder({ ...data, openId }, cloud);
  } catch (e) {
    return { isOk: 0, msg: '铺捉到错误' + e };
  }
};


/*
* 系订单
* @return data: object 埋点的数据
* */
exports.getOrderItem = async (context) => {
  const { cloud, data, mixNick, openId } = context
  try {
    let date = new Date()
    return await service.getOrderItem({ ...data, openId }, cloud);
  } catch (e) {
    return { isOk: 0, msg: '铺捉到错误' + e };
  }
};






/**
 * 查询订单
 * @param {nickName: string} 明文nick
 * @param {session: string} 
 */
exports.sellerGet = async (context) => {

  try {
    const result = await context.cloud.topApi.invoke({
      api: 'taobao.item.seller.get',
      // autoSession: "true",
      data: {
        'fields': 'num_iid,title,nick,price,approve_status,sku',
        'num_iid': '635852017485',
        session: "610201247e4defaa9d2918a36fd159db1b46ef22263d2121928865133",
      }
    });
    return result
  } catch (err) {
    return err
  }
};


exports.getPageData = async (context) => {
  const { data } = context
  if (!data.page) {
    return [
      [{
        itemid: 540665192100,
        skuid: 3243061864527,
        w: '57rpx',
        d: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/tab/tab1/tab-btn-1-1.png?t=123",
        goods: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/tab/tab1/tab-content-1-1.png?t=123",
        goods_w: '404rpx',
        goods_h: '457rpx',
        x: "0rpx",
        y: "40rpx",
        price: 780,
        name: "洁面泡沫",
        ml: " / 125ml",
        animate: "",
        xx: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/jiesuan/1-1.png?t=123"
      },
      {
        itemid: 544910934579,
        skuid: 3728381369988,
        w: '45rpx',
        x: "35rpx",
        y: "40rpx",
        goods_w: '406rpx',
        goods_h: '485rpx',
        d: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/tab/tab1/tab-btn-1-2.png?t=123",
        goods: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/tab/tab1/tab-content-1-2.png?t=123",
        animate: "",
        price: 830,
        name: "精萃液",
        ml: " / 100ml",
        xx: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/jiesuan/1-2.png?t=123"
      },
      {
        itemid: 539778324602,
        skuid: 3235413717409,
        w: '41rpx',
        x: "35rpx",
        y: "40rpx",
        goods_w: '390rpx',
        goods_h: '487rpx',
        d: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/tab/tab1/tab-btn-1-3.png?t=123",
        goods: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/tab/tab1/tab-content-1-3.png?t=123",
        animate: "",
        price: 780,
        name: "焕肤水",
        ml: " / 200ml",
        xx: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/jiesuan/1-3.png?t=123"
      }],
      [{
        itemid: 611898588330,
        skuid: 4308565904751,
        w: '83rpx',
        x: "0rpx",
        y: "40rpx",
        goods_w: '477rpx',
        goods_h: '497rpx',
        d: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab2/tab-btn-2-1.png?t=123",
        goods: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab2/tab-content-2-1.png?t=123",
        price: 1780,
        name: "浓缩修护眼霜",
        ml: " / 15ml",
        animate: "",
        xx: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/jiesuan/2-1.png?t=123"
      },
      {
        itemid: 537657109956,
        skuid: 3209604299391,
        w: '37rpx',
        x: "50rpx",
        y: "40rpx",
        goods_w: '416rpx',
        goods_h: '458rpx',
        d: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab2/tab-btn-2-2.png?t=123",
        goods: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab2/tab-content-2-2.png?t=123",
        price: 1570,
        name: "修护精华露",
        ml: " / 15ml",
        animate: "",
        xx: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/jiesuan/2-2.png?t=123"
      },
      {
        itemid: 539973120593,
        skuid: 3894037090934,
        w: '35rpx',
        x: "50rpx",
        y: "40rpx",
        goods_w: '404rpx',
        goods_h: '457rpx',
        d: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab2/tab-btn-2-3.png?t=123",
        goods: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab2/tab-content-2-3.png?t=123",
        animate: "",
        price: 1160,
        name: "臻璨焕活油",
        ml: " / 15ml",
        xx: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/jiesuan/2-3.png?t=123"
      }],
      [{
        itemid: 522639774122,
        skuid: 4342002634621,
        w: '72rpx',
        x: "25rpx",
        y: "40rpx",
        goods_w: '408rpx',
        goods_h: '457rpx',
        d: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab3/tab-btn-3-1.png?t=123",
        goods: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab3/tab-content-3-1.png?t=123",
        price: 800,
        name: "精华面霜",
        ml: " / 15ml",
        animate: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab3/tab-content-3-b.png?t=123",
        animate_w: "215rpx",
        animate_h: "213rpx",
        animate_y: "80rpx",
        xx: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/jiesuan/3-1.png?t=123"
      },
      {
        itemid: 522638865095,
        skuid: 3111443529429,
        w: '72rpx',
        x: "25rpx",
        y: "40rpx",
        goods_w: '376rpx',
        goods_h: '457rpx',
        d: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab3/tab-btn-3-3.png?t=123",
        goods: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab3/tab-content-3-3.png?t=123",
        price: 800,
        name: "精华乳霜",
        ml: " / 15ml",
        animate: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab3/tab-content-3-b-3.png?t=123",
        animate_w: "219rpx",
        animate_h: "230rpx",
        animate_y: "80rpx",
        xx: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/jiesuan/3-3.png?t=123"
      }, {
        itemid: 565477253790,
        skuid: 4696662002705,
        w: '72rpx',
        x: "25rpx",
        y: "40rpx",
        goods_w: '376rpx',
        goods_h: '457rpx',
        d: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab3/tab-btn-3-2.png?t=123",
        goods: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab3/tab-content-3-2.png?t=123",
        price: 800,
        name: "精华凝霜",
        ml: " / 15ml",
        animate: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab3/tab-content-3-b-2.png?t=123",
        animate_w: "212rpx",
        animate_h: "191rpx",
        animate_y: "80rpx",
        xx: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/jiesuan/3-2.png?t=123"
      }]
    ]
  } else {
    return [
      [{
        itemid: 540665192100,
        skuid: 3243061864527,
        w: '57rpx',
        d: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/tab/tab1/tab-btn-1-1.png?t=123",
        goods: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/tab/tab1/tab-content-1-1.png?t=123",
        goods_w: '404rpx',
        goods_h: '457rpx',
        x: "0rpx",
        y: "40rpx",
        price: 780,
        name: "洁面泡沫",
        ml: " / 125ml",
        animate: "",
        xx: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/jiesuan/1-1.png?t=123"
      },
      {
        itemid: 544910934579,
        skuid: 3728381369988,
        w: '45rpx',
        x: "35rpx",
        y: "40rpx",
        goods_w: '406rpx',
        goods_h: '485rpx',
        d: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/tab/tab1/tab-btn-1-2.png?t=123",
        goods: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/tab/tab1/tab-content-1-2.png?t=123",
        animate: "",
        price: 830,
        name: "精萃液",
        ml: " / 100ml",
        xx: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/jiesuan/1-2.png?t=123"
      },
      {
        itemid: 539778324602,
        skuid: 3235413717409,
        w: '41rpx',
        x: "35rpx",
        y: "40rpx",
        goods_w: '390rpx',
        goods_h: '487rpx',
        d: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/tab/tab1/tab-btn-1-3.png?t=123",
        goods: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/tab/tab1/tab-content-1-3.png?t=123",
        animate: "",
        price: 780,
        name: "焕肤水",
        ml: " / 200ml",
        xx: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/jiesuan/1-3.png?t=123"
      }],
      [{
        itemid: 611898588330,
        skuid: 4308565904751,
        w: '83rpx',
        x: "0rpx",
       y: "40rpx",
        goods_w: '477rpx',
        goods_h: '497rpx',
        d: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab2/tab-btn-2-1.png?t=123",
        goods: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab2/tab-content-2-1.png?t=123",
        price: 1780,
        name: "浓缩修护眼霜",
        ml: " / 15ml",
        animate: "",
        xx: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/jiesuan/2-1.png?t=123"
      },
      {
        itemid: 537657109956,
        skuid: 3209604299391,
        w: '37rpx',
        x: "50rpx",
        y: "40rpx",
        goods_w: '416rpx',
        goods_h: '458rpx',
        d: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab2/tab-btn-2-2.png?t=123",
        goods: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab2/tab-content-2-2.png?t=123",
        price: 1570,
        name: "修护精华露",
        ml: " / 15ml",
        animate: "",
        xx: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/jiesuan/2-2.png?t=123"
      },
      {
        itemid: 539973120593,
        skuid: 3894037090934,
        w: '35rpx',
        x: "50rpx",
        y: "40rpx",
        goods_w: '404rpx',
        goods_h: '457rpx',
        d: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab2/tab-btn-2-3.png?t=123",
        goods: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab2/tab-content-2-3.png?t=123",
        animate: "",
        price: 1160,
        name: "臻璨焕活油",
        ml: " / 15ml",
        xx: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/jiesuan/2-1.png?t=123"
      }],
      [{
        itemid: 522639774122,
        skuid: 4342002634621,
        w: '72rpx',
        x: "25rpx",
        y: "40rpx",
        goods_w: '408rpx',
        goods_h: '457rpx',
        d: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab3/tab-btn-3-1.png?t=123",
        goods: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab3/tab-content-3-1.png?t=123",
        price: 800,
        name: "精华面霜",
        ml: " / 15ml",
        animate: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab3/tab-content-3-b.png?t=123",
        animate_w: "215rpx",
        animate_h: "213rpx",
        animate_y: "80rpx",
        xx: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/jiesuan/3-1.png?t=123"
      },
      {
        itemid: 522638865095,
        skuid: 3111443529429,
        w: '72rpx',
        x: "25rpx",
        y: "40rpx",
        goods_w: '376rpx',
        goods_h: '457rpx',
        d: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab3/tab-btn-3-3.png?t=123",
        goods: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab3/tab-content-3-3.png?t=123",
        price: 800,
        name: "精华乳霜",
        ml: " / 15ml",
        animate: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab3/tab-content-3-b-3.png?t=123",
        animate_w: "219rpx",
        animate_h: "230rpx",
        animate_y: "80rpx",
        xx: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/jiesuan/3-3.png?t=123"
      }, {
        itemid: 565477253790,
        skuid: 4696662002705,
        w: '72rpx',
        x: "25rpx",
        y: "40rpx",
        goods_w: '376rpx',
        goods_h: '457rpx',
        d: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab3/tab-btn-3-2.png?t=123",
        goods: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab3/tab-content-3-2.png?t=123",
        price: 800,
        name: "精华凝霜",
        ml: " / 15ml",
        animate: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/1334/tab3/tab-content-3-b-2.png?t=123",
        animate_w: "212rpx",
        animate_h: "191rpx",
        animate_y: "80rpx",
        xx: "https://morefun-active.oss-cn-beijing.aliyuncs.com/LAMER_Combination_shopping/jiesuan/3-2.png?t=123"
      }]
    ]
  }
};




